package com.borqsprocessmanager;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.borqsprocessmanager.util.Log;
import com.borqsprocessmanager.util.MathUtil;
import com.borqsprocessmanager.util.PowerProfileUtil;
import com.borqsprocessmanager.util.PreferencesUtil;
import com.umeng.analytics.MobclickAgent;
import com.umeng.analytics.onlineconfig.UmengOnlineConfigureListener;

public class ConfigParams {
    private static final String TAG = "ConfigParams";

    private static final String CPU_TOTAL_BASELINE_KEY = "cpu_total_baseline_key";
    private static final String CPU_PROCESS_BASELINE_KEY = "cpu_process_baseline_key";
    private static final String NETWORK_BASELINE_KEY = "network_baseline_key";
    private static final String SIGNAL_BASE_LINE_KEY = "signal_base_line_key";
    private static final String SREEN_ON_BASE_LINE_KEY = "sreen_on_base_line_key";
    private static final String SREEN_OFF_WORKING_TIME_KEY = "sreen_off_working_time_key";
    private static final String POWER_MAX_TIME_BASELINE_KEY = "power_consumption_rate_baseline_key";
    private static final String POWER_RATE_BASELINE_KEY = "power_rate_baseline_key";
    private static final String BATTERY_STANDARD_KEY = "battery_standard_key";

    private static final String IS_NEW_MESSAGE_KEY = "is_new_message_key";
    private static final String MESSAGE_KEY = "message_key";

    public static long POWER_MIN_TIME_BASELINE = 30 * 60 * 1000;
    public static int POWER_BASELINE = 3600000;
    public static float POWER_RATE_BASELINE = 0.75f;

    public static float CPU_TOTAL_BASELINE = 4.0f;
    public static float CPU_PROCESS_BASELINE = 4.0f;

    public static int NETWORK_BASELINE = 50 * 1024;// bye
    public static long SIGNAL_BASELINE = 30 * 60 * 1000;

    public static long SREEN_ON_BASE_LINE = 4 * 60 * 1000;
    public static long SREEN_OFF_WORKING_TIME = 10 * 1000;// killbackgroud App
                                                          // delay time
    public static int BATTERY_STANDARD = 40;

    public static int IS_NEW_MESSAGE = 1;
    public static String MESSAGE = "";

    public static void updataConfig(Context context) {
        MobclickAgent
                .setOnlineConfigureListener(new UmengOnlineConfigureListener() {
                    @Override
                    public void onDataReceived(JSONObject data) {
                        try {
                            if (data != null) {
                                updataConfig(data);
                                saveConfigToPreferences();
                                return;
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
        getConfigToPreferences();
        Log.v(TAG, "BATTERY_STANDARD===" + BATTERY_STANDARD);
        Log.v(TAG, "CPU_PROCESS_BASELINE==" + CPU_PROCESS_BASELINE);
        Log.v(TAG, "NETWORK_BASELINE==" + NETWORK_BASELINE);
        Log.v(TAG, "SIGNAL_BASE_LINE==" + SIGNAL_BASELINE);
        Log.v(TAG, "POWER_CONSUMPTION_RATE_BASELINE=="
                + POWER_MIN_TIME_BASELINE);
        Log.v(TAG, "POWER_RATE_BASELINE==" + POWER_RATE_BASELINE);
    }

    private static void updataConfig(JSONObject data)
            throws NumberFormatException, JSONException {
        Log.v(TAG, "updataConfig");
        CPU_TOTAL_BASELINE = Float.parseFloat(data
                .getString(CPU_TOTAL_BASELINE_KEY));
        Log.v(TAG, "CPU_TOTAL_BASELINE==" + CPU_TOTAL_BASELINE);
        CPU_PROCESS_BASELINE = Float.parseFloat(data
                .getString(CPU_PROCESS_BASELINE_KEY));

        NETWORK_BASELINE = (Integer.parseInt(data
                .getString(NETWORK_BASELINE_KEY))) * 1024;
        SIGNAL_BASELINE = (Long.parseLong(data.getString(SIGNAL_BASE_LINE_KEY))) * 60 * 1000;
        SREEN_ON_BASE_LINE = (Long.parseLong(data
                .getString(SREEN_ON_BASE_LINE_KEY))) * 60 * 1000;
        SREEN_OFF_WORKING_TIME = (Long.parseLong(data
                .getString(SREEN_OFF_WORKING_TIME_KEY))) * 60 * 1000;
        BATTERY_STANDARD = Integer.parseInt(data
                .getString(BATTERY_STANDARD_KEY));
        Log.v(TAG, "BATTERY_STANDARD===" + BATTERY_STANDARD);
        POWER_RATE_BASELINE = Float.parseFloat(data
                .getString(POWER_RATE_BASELINE_KEY));

        IS_NEW_MESSAGE = Integer.parseInt(data.getString(IS_NEW_MESSAGE_KEY));
        // MESSAGE = data.getString(MESSAGE_KEY);

        POWER_MIN_TIME_BASELINE = initPowerConsumptionRateBaseline(BATTERY_STANDARD);

    }

    private static void saveConfigToPreferences() {
        PreferencesUtil pf = PreferencesUtil.getInstance(App.mContext);
        pf.putFloat(CPU_TOTAL_BASELINE_KEY, CPU_TOTAL_BASELINE);
        pf.putFloat(CPU_PROCESS_BASELINE_KEY, CPU_PROCESS_BASELINE);
        pf.putFloat(POWER_RATE_BASELINE_KEY, POWER_RATE_BASELINE);
        pf.putInt(NETWORK_BASELINE_KEY, NETWORK_BASELINE);
        pf.putLong(SIGNAL_BASE_LINE_KEY, SIGNAL_BASELINE);
        pf.putLong(SREEN_ON_BASE_LINE_KEY, SREEN_ON_BASE_LINE);
        pf.putLong(SREEN_OFF_WORKING_TIME_KEY, SREEN_OFF_WORKING_TIME);
        pf.putLong(POWER_MAX_TIME_BASELINE_KEY, POWER_MIN_TIME_BASELINE);
        pf.putInt(IS_NEW_MESSAGE_KEY, IS_NEW_MESSAGE);
        // pf.putString(MESSAGE_KEY, MESSAGE);
    }

    private static void getConfigToPreferences() {
        PreferencesUtil pf = PreferencesUtil.getInstance(App.mContext);
        CPU_TOTAL_BASELINE = pf.getFloat(CPU_TOTAL_BASELINE_KEY,
                CPU_TOTAL_BASELINE);
        CPU_PROCESS_BASELINE = pf.getFloat(CPU_PROCESS_BASELINE_KEY,
                CPU_PROCESS_BASELINE);
        NETWORK_BASELINE = pf.getInt(NETWORK_BASELINE_KEY, NETWORK_BASELINE);
        SIGNAL_BASELINE = pf.getLong(SIGNAL_BASE_LINE_KEY, SIGNAL_BASELINE);
        SREEN_ON_BASE_LINE = pf.getLong(SREEN_ON_BASE_LINE_KEY,
                SREEN_ON_BASE_LINE);
        SREEN_OFF_WORKING_TIME = pf.getLong(SREEN_OFF_WORKING_TIME_KEY,
                SREEN_OFF_WORKING_TIME);

        POWER_RATE_BASELINE = pf.getFloat(POWER_RATE_BASELINE_KEY,
                POWER_RATE_BASELINE);
        POWER_MIN_TIME_BASELINE = pf.getLong(POWER_MAX_TIME_BASELINE_KEY,
                POWER_MIN_TIME_BASELINE);
        IS_NEW_MESSAGE = pf.getInt(IS_NEW_MESSAGE_KEY, IS_NEW_MESSAGE);
        MESSAGE = App.mContext.getString(R.string.message_dialog_text);
        // MESSAGE = pf.getString(MESSAGE);
    }

    public static void setIsNewMessage(int isNewMessage) {
        PreferencesUtil pf = PreferencesUtil.getInstance(App.mContext);
        pf.putInt(IS_NEW_MESSAGE_KEY, isNewMessage);
    }

    private static long initPowerConsumptionRateBaseline(int batteryStandard) {
        PowerProfileUtil ppuitl = new PowerProfileUtil();
        Log.v(TAG, "new PowerProfileUtil");
        float BatteryCapacit = ppuitl.getBatteryCapacity();
        Log.v(TAG, "BatteryCapacit===" + BatteryCapacit);
        float BatteryCapacitPercent = BatteryCapacit / 100;
        Log.v(TAG, "BatteryCapacitPercent===" + BatteryCapacitPercent);
        Log.v(TAG, "batteryStandard===" + batteryStandard);
        float restime = (float) MathUtil.divide(BatteryCapacitPercent,
                batteryStandard, 1);
        Log.v(TAG, "restime===" + restime);
        long baseline = (long) (restime * 60 * 60 * 1000);
        return baseline;
    }

}
